package com.example.image.glide

import android.content.Context
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory
import com.bumptech.glide.load.engine.cache.LruResourceCache
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator
import com.bumptech.glide.module.AppGlideModule

/**
 * Author: Jason Zhang
 * Date: 2025/1/13
 * Desc: 自定义Glide的内存和磁盘缓存大小
 */

@GlideModule
class GlideModule : AppGlideModule(){

    override fun applyOptions(context: Context, builder: GlideBuilder) {
        super.applyOptions(context, builder)
        //1.修改内存缓存大小
        var memorySize = MemorySizeCalculator.Builder(context).build().memoryCacheSize / 6L //Glide默认内存缓存大小是当前APP应用内存的1/8大小
        builder.setMemoryCache(LruResourceCache(memorySize))

        //2.修改磁盘缓存大小
        builder.setDiskCache(InternalCacheDiskCacheFactory(context, "glideDiskCache", 300 * 1024 * 1024L)) //Glide默认磁盘缓存大小为250M
    }


}